package com.xydtech.ecard.product.controller;


import com.xydtech.common.annotation.PassToken;
import com.xydtech.common.enums.ResultStatus;
import com.xydtech.common.model.ResponseModel;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.ecard.controller.EcardApiBaseController;
import com.xydtech.ecard.product.domain.EcardApiPrdInfo;
import com.xydtech.ecard.product.service.IEcardApiPrdInfoService;
import com.xydtech.ecard.user.domain.EcardApiUserInfo;
import com.xydtech.ecard.user.service.IEcardApiUserService;
import com.xydtech.system.domain.SysWechatApplet;
import com.xydtech.system.service.ISysWechatAppletService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Objects;

/**
 * 电子名片产品配置Controller
 *
 * @author Daniel
 * @date 2021-09-06
 */
@Controller
@Api(tags = "电子名片-产品配置")
@RequestMapping("/ecardSaasApi/config/prd")
public class EcardApiPrdInfoController extends EcardApiBaseController {
    private String prefix = "config/prd";

    @Autowired
    private IEcardApiPrdInfoService ecardApiPrdInfoService;
    @Autowired
    private ISysWechatAppletService wechatAppletService;
    @Autowired
    private IEcardApiUserService ecardApiUserService;

    /**
     * 电子名片-产品配置-获取员工的优选产品
     * @return
     */
    @PassToken
    @GetMapping("/getPersonalHotPrd")
    @ApiOperation(value = "电子名片-产品配置-获取员工的优选产品", notes = "电子名片-产品配置-获取员工的优选产品")
    @ApiResponses({@ApiResponse(code = 0, message = "OK", response = EcardApiPrdInfo.class)})
    @ResponseBody
    public ResponseModel getPersonalHotPrd(String custmanagerId) {
        String salt = getSalt();//盐值
        if (StringUtils.isEmpty(custmanagerId)) {
            custmanagerId = getCurrentUserId();
        }
        String currentUserId = getCurrentUserId();
        EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
        //当前小程序id
        SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());
        String appId = wechatApplet.getAppid();
        if (StringUtils.isEmpty(appId)) {
            return ResponseModel.error(ResultStatus.PARAMETER_ERROR);
        }

        List<EcardApiPrdInfo> ecardApiPrdInfoList = ecardApiPrdInfoService.getPrdListByUser(custmanagerId, wechatApplet.getDeptId());
        return ResponseModel.ok(ecardApiPrdInfoList);
    }

    /**
     * 电子名片-产品配置-获取指定员工的优选产品
     * @return
     */
    @PassToken
    @GetMapping("/getCustManagerHotPrd/{custmanagerId}")
    @ApiOperation(value = "电子名片-产品配置-获取指定员工的优选产品", notes = "电子名片-产品配置-获取指定员工的优选产品")
    @ApiImplicitParams({@ApiImplicitParam(name = "custmanagerId", value = "客户经理ID", dataType = "String", dataTypeClass = String.class, paramType = "query", required = true)})
    @ApiResponses({@ApiResponse(code = 0, message = "OK", response = EcardApiPrdInfo.class)})
    @ResponseBody
    public ResponseModel getCustManagerHotPrd(@PathVariable("custmanagerId") String custmanagerId) {
        String salt = getSalt();//盐值
        String currentUserId = getCurrentUserId();
        EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
        //当前小程序id
        SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());
        String appId = wechatApplet.getAppid();
        if (StringUtils.isEmpty(appId)) {
            return ResponseModel.error(ResultStatus.PARAMETER_ERROR);
        }

        List<EcardApiPrdInfo> ecardApiPrdInfoList = ecardApiPrdInfoService.getPrdListByUser(custmanagerId, wechatApplet.getDeptId());
        return ResponseModel.ok(ecardApiPrdInfoList);
    }

    /**
     * 电子名片-工作室-获取产品详情
     * @return
     */
    @GetMapping("/getPrdDetailInfoByPrdId/{prdId}/{registId}")
    @ApiOperation(value = "电子名片-工作室-获取产品详情", notes = "电子名片-工作室-获取产品详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "prdId", value = "产品ID", dataType = "Long", dataTypeClass = Long.class, paramType = "query", required = true),
            @ApiImplicitParam(name = "registId", value = "客户经理注册ID", dataType = "String", dataTypeClass = String.class, paramType = "query", required = false)
    })
    @ApiResponses({@ApiResponse(code = 0, message = "OK", response = EcardApiPrdInfo.class)})
    @ResponseBody
    public ResponseModel getPrdDetailByPrdId(@PathVariable("prdId") Long prdId, @PathVariable(value = "registId", required = false) String registId) {

        if (Objects.isNull(prdId)) {
            return ResponseModel.error("产品ID不能为空！");
        }
        if (StringUtils.isNullOrEmpty(registId)) {
            registId = getCurrentUserId();
        }
        //获取产品的详情
        EcardApiPrdInfo prdInfo = ecardApiPrdInfoService.getPrdDetailByPrdId(prdId, registId);

        return ResponseModel.ok(prdInfo);
    }

    /**
     * 电子名片-工作室-新增产品分享记录
     * @return
     */
    @GetMapping("/insertSharePrdInfo")
    @ApiOperation(value = "电子名片-工作室-新增产品分享记录", notes = "电子名片-工作室-新增产品分享记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "prdId", value = "产品ID", dataType = "Long", dataTypeClass = Long.class, paramType = "query", required = true),
            @ApiImplicitParam(name = "custmanagerId", value = "客户经理的ID", dataType = "Long", dataTypeClass = Long.class, paramType = "query", required = true)
    })
    @ApiResponses({@ApiResponse(code = 0, message = "OK", response = ResponseModel.class)})
    @ResponseBody
    public ResponseModel insertSharePrdInfo(Long prdId, Long custmanagerId) {
        if (Objects.isNull(prdId) || Objects.isNull(custmanagerId)) {
            return ResponseModel.error(ResultStatus.PARAMETER_ERROR);
        }

        //当前登录人
        String currentUserId = getCurrentUserId();

        //新增产品分享记录
        int saveCount = ecardApiPrdInfoService.insertSharePrdInfo(currentUserId, prdId, custmanagerId);
        return saveCount > 0 ? ResponseModel.ok() : ResponseModel.error("新增产品分享记录失败！");
    }
}
